package com.example.demo.controller;

import com.example.demo.entity.DataUser;
import com.example.demo.service.DataUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guochao
 * @version 1.0
 * @date 2022/5/26 1:12
 */
@Controller
public class BatchAddDataController {
    @Resource
    private DataUserService dataUserService;

    /**
     * 添加单条数据
     * http://127.0.0.1:8080/addDataUser?name=123
     * @param dataUser
     * @return
     */
    @RequestMapping("/addDataUser")
    @ResponseBody
    public Object addDataUser(DataUser dataUser) {
        long start = System.currentTimeMillis();
        dataUserService.addDataUser(dataUser);
        return (System.currentTimeMillis() - start);
    }

    /**
     * 批量添加数据
     * http://127.0.0.1:8080/addDataUserBatch?len=5000000
     * @param len
     * @return
     */
    @RequestMapping("/addDataUserBatch")
    @ResponseBody
    public Object addDataUserBatch(@RequestParam Integer len) {
        long start = System.currentTimeMillis();
        if (len > 50000) {
            int l = len / 50000;
            for (int i = 0; i < l; i++) {
                List<DataUser> list = new ArrayList<>();
                for (int j = 0; j < 50000; j++) {
                    DataUser dataUser = new DataUser();
                    dataUser.setName(((i + 1) * (j + 1)) + "");
                    list.add(dataUser);
                }
                dataUserService.addDataUserBatch(list);
            }
        } else {
            List<DataUser> list = new ArrayList<>();
            for (int i = 0; i < len; i++) {
                DataUser dataUser = new DataUser();
                dataUser.setName((i + 1) + "");
                list.add(dataUser);
            }
            dataUserService.addDataUserBatch(list);
        }
        return (System.currentTimeMillis() - start);
    }

    /**
     * 查询数据
     * http://127.0.0.1:8080/getDataUserList?name=1
     * http://127.0.0.1:8080/getDataUserList
     * @param dataUser
     * @return
     */
    @RequestMapping("/getDataUserList")
    @ResponseBody
    public Object getDataUserList(DataUser dataUser) {
        long start = System.currentTimeMillis();
        dataUserService.getDataUserList(dataUser);
        return (System.currentTimeMillis() - start);
    }

    /**
     * 修改数据
     * http://127.0.0.1:8080/updateDataUser?id=2&name=123
     * @param dataUser
     * @return
     */
    @RequestMapping("/updateDataUser")
    @ResponseBody
    public Object updateDataUser(DataUser dataUser) {
        long start = System.currentTimeMillis();
        dataUserService.updateDataUser(dataUser);
        return (System.currentTimeMillis() - start);
    }

    /**
     * 删除单个数据
     * http://127.0.0.1:8080/deleteDataUserById?id=1
     * @param id
     * @return
     */
    @RequestMapping("/deleteDataUserById")
    @ResponseBody
    public Object deleteDataUserById(Long id) {
        long start = System.currentTimeMillis();
        dataUserService.deleteDataUserById(id);
        return (System.currentTimeMillis() - start);
    }

    /**
     * 删除多个数据
     * http://127.0.0.1:8080/deleteDataUserByIds?ids=2,3
     * @param ids
     * @return
     */
    @RequestMapping("/deleteDataUserByIds")
    @ResponseBody
    public Object deleteDataUserByIds(Long[] ids) {
        long start = System.currentTimeMillis();
        dataUserService.deleteDataUserByIds(ids);
        return (System.currentTimeMillis() - start);
    }
}
